package com.yehui.algorithm.other;

import java.util.Arrays;

/**
 * @author 业徽
 * @date 2021/2/8
 */
public class Train {

    /**
     * 火车进站
     *
     * @return 可能的出站顺序
     */
    public static String[] train(int[] source) {
        if (source.length > 0) {
            if (source.length == 1) {
                return new String[]{String.valueOf(source[0])};
            } else {
                String[] subResult = train(Arrays.copyOfRange(source, 1, source.length));
                String[] result = new String[subResult.length * 2];
                int i = 0;
                for (; i < subResult.length; i++) {
                    result[i] = source[0] + subResult[i];
                }
                for (; i < result.length; i++) {
                    result[i] = subResult[i - subResult.length] + source[0];
                }
                return result;
            }
        }

        return null;
    }

}

